﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Kkbac.Tools.Web.MVC.ActionResults
{
    using Extensions.HttpContexts;

    public class MessageResult : ActionResult
    {
        public string Message { get; set; }
        public bool IsHistoryBack { get; set; }
        public string BackUrl { get; set; }
        public RouteValueDictionary ValuesDictionary { get; set; }

        public MessageResult(string message)
        {
            this.Message = message;
            this.ValuesDictionary = null;
        }

        public MessageResult(string message, bool isHistoryBack)
        {
            this.Message = message;
            this.IsHistoryBack = isHistoryBack;
        }

        //public AlertResult(string message, string backUrl)
        //{
        //    this.Message = message;
        //    this.BackUrl = backUrl;
        //}

        public MessageResult(string message, object routeValues)
            : this(message, null, null, routeValues)
        {
        }

        public MessageResult(string message, string actionName) :
            this(message, actionName, null, null)
        {
        }

        public MessageResult(string message, string actionName, object routeValues) :
            this(message, actionName, null, routeValues)
        {
        }

        public MessageResult(string message, string actionName, string controllerName, object routeValues)
        {
            this.Message = message;
            this.ValuesDictionary = new RouteValueDictionary(routeValues);
            if (actionName != null)
            {
                this.ValuesDictionary["action"] = actionName;
            }
            if (controllerName != null)
            {
                this.ValuesDictionary["controller"] = controllerName;
            }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            Message = Message.Replace("\"", "'");
            var script = "";

            if (IsHistoryBack == true)
            {
                script = string.Format("<script>alert(\"{0}\");window.history.go(-1);</script>",
                      Message);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(BackUrl) == true)
                {
                    if (this.ValuesDictionary != null)
                    {
                        BackUrl = context.RequestContext.GetUrl(this.ValuesDictionary);
                    }
                    else
                    {
                        var urlReferrer = context.HttpContext.Request.UrlReferrer;
                        if (urlReferrer != null)
                        {
                            BackUrl = urlReferrer.PathAndQuery;
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(BackUrl) == true)
                {
                    BackUrl = "/";
                }
                script = string.Format("<script>alert(\"{0}\");window.location.href = \"{1}\";</script>",
                     Message, BackUrl);
            }

            context.HttpContext.Response.Write(script);

        }
    }
}
